package com.evil.account.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.evil.account.pojo.entity.UserEnterprise;
import com.evil.common.account.dto.enterprise.EnterpriseManagerPageReqDTO;
import com.evil.common.account.dto.user.CheckEnterpriseUserIdsReqDTO;
import com.evil.common.account.dto.user.FindUserIdsByIdReqDTO;
import com.evil.common.account.dto.user.UserBaseRespDTO;
import com.evil.common.account.dto.user.enterprise.MineEnterprisesReqDTO;
import com.evil.common.core.entity.QueryModel;
import com.evil.common.core.enums.RCodeEnum;
import com.evil.common.core.enums.SwitchEnum;
import com.evil.common.core.exception.BusinessException;
import com.evil.common.db.mapper.BatchMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;

import java.util.Collection;
import java.util.List;
import java.util.Optional;

/**
 * 用户企业表
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
public interface UserEnterpriseMapper extends BaseMapper<UserEnterprise>, BatchMapper<UserEnterprise, Long> {

    int updateBatch(List<UserEnterprise> list);

    int batchInsert(@Param("list") List<UserEnterprise> list);

    default Optional<UserEnterprise> isExistById(Long id) {
        return Optional.ofNullable(this.selectById(id));
    }

    default UserEnterprise findById(Long id) {
        return this.isExistById(id).orElseThrow(() -> new BusinessException(RCodeEnum.USER_ENTERPRISE_NOT_EXIST));
    }

    List<UserEnterprise> findByIds(@Param("ids") Collection<Long> ids, @Param("filterDeleted") boolean filterDeleted);

    Optional<UserEnterprise> isExistByEIdAndUId(@Param("enterpriseId") Long enterpriseId, @Param("userId") Long userId);

    default UserEnterprise findByEIdAndUId(Long enterpriseId, Long userId) {
        return this.isExistByEIdAndUId(enterpriseId, userId).orElseThrow(() -> new BusinessException(RCodeEnum.USER_ENTERPRISE_NOT_EXIST));
    }

    default Optional<UserEnterprise> isExistNormalByEIdAndUId(@Param("enterpriseId") Long enterpriseId, @Param("userId") Long userId) {
        return this.isExistByEIdAndUId(enterpriseId, userId).map(e -> {
            if (!SwitchEnum.isOpen(e.getIsDeleted())) {
                throw new BusinessException(RCodeEnum.USER_ENTERPRISE_DELETED);
            }
            if (!SwitchEnum.isOpen(e.getIsDeleted())) {
                throw new BusinessException(RCodeEnum.USER_ENTERPRISE_DISABLED);
            }
            return e;
        });
    }

    default UserEnterprise findNormalByEIdAndUId(@Param("enterpriseId") Long enterpriseId, @Param("userId") Long userId) {
        return this.isExistNormalByEIdAndUId(enterpriseId, userId).orElseThrow(() -> new BusinessException(RCodeEnum.USER_ENTERPRISE_NOT_EXIST));
    }

    Optional<UserEnterprise> findPlatformDefaultByUserId(@Param("userId") Long userId, @Param("platformType") Integer platformType);

    Optional<UserEnterprise> findPlatformOneByUserId(@Param("userId") Long userId, @Param("platformType") Integer platformType);

    List<UserEnterprise> findUserEnterprises(@Param("userId") Long userId, @Param("param") MineEnterprisesReqDTO mineEnterprisesReqDTO);

    List<UserEnterprise> findEnterpriseUsers(@Param("enterpriseId") Long enterpriseId);

    List<UserEnterprise> findEnterpriseUsersByEIdAndUIds(@Param("enterpriseId") Long enterpriseId,
                                                         @Param("userIds") Collection<Long> userIds,
                                                         @Param("filterDeleted") boolean filterDeleted);

    List<UserBaseRespDTO> enterpriseManagerList(EnterpriseManagerPageReqDTO enterpriseManagerPageReqDTO);

    default PageInfo<UserBaseRespDTO> enterpriseManagerPage(QueryModel<EnterpriseManagerPageReqDTO> queryModel) {
        return PageHelper.startPage(queryModel.getPageNum(), queryModel.getPageSize(), queryModel.initOrder())
                .doSelectPageInfo(() -> this.enterpriseManagerList(queryModel.getParam()));
    }

    List<UserBaseRespDTO> enterpriseUserBases(FindUserIdsByIdReqDTO findUserIdsByIdReqDTO);

    List<Long> enterpriseUserIds(FindUserIdsByIdReqDTO findUserIdsByIdReqDTO);

    List<Long> checkEnterpriseUserIds(CheckEnterpriseUserIdsReqDTO checkEnterpriseUserIdsReqDTO);
}